﻿Imports System.Web.UI
Imports System.Collections.Generic
Imports System.Reflection

Imports DotNetNuke
Imports DotNetNuke.Services.Exceptions
Imports DotNetNuke.Services.Localization
Imports DotNetNuke.Security.Roles

Namespace dnnWerk.Modules.Nuntio.Content

    Partial Class uc_Options
        Inherits ContentBase


#Region "Delegates"

        Public Delegate Sub ContentenChangedEventHandler(ByVal sender As Object, ByVal e As RoleChangedEventArgs)
        Public Delegate Sub RoleChangedEventHandler(ByVal sender As Object, ByVal e As RoleChangedEventArgs)

#End Region

#Region "Events"

        Public Event ContentChanged As ContentenChangedEventHandler
        Public Event RoleChanged As RoleChangedEventHandler

#End Region

#Region "Private Members"

        Public Overloads ReadOnly Property LocalResourceFile() As String
            Get
                Return Me.ModuleDirectory & "controls/App_LocalResources/uc_Options.ascx"
            End Get
        End Property

        Private _identifier As String
        Private _selectedRole As String = "GENERIC"
        Private _moduleId As Integer

#End Region

#Region "Public Members"

        Public Shadows Property ModuleId() As Integer
            Get
                Return _moduleid
            End Get
            Set(ByVal value As Integer)
                _moduleid = value
            End Set
        End Property

        Public Property Identifier() As String
            Get
                Return _identifier
            End Get
            Set(ByVal value As String)
                _identifier = value
            End Set
        End Property

        Public Property SelectedRole() As String
            Get

                Try
                    _selectedRole = cboRoles.SelectedValue
                Catch
                End Try

                If _selectedRole = "" Then
                    _selectedRole = "GENERIC"
                End If

                Return _selectedRole

            End Get
            Set(ByVal value As String)
                _selectedRole = value
            End Set
        End Property

#End Region

#Region "Event Handlers"

        Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
            'fix fck update issue
            Framework.AJAX.RegisterPostBackControl(cboRoles)
        End Sub

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            If Not Page.IsPostBack Then
                BindRoles()
                BindContentGrid()
            End If

            LocalizeForm()

        End Sub

#End Region

#Region "Private Methods"


        Private Sub LocalizeForm()
            'lblLocales.Text = Localization.GetString("lblLocales", _resourcefile)
        End Sub

        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        '
        ' used to bind roles to the newsletter checkboxes 
        ' and alternative roles content on the advanced tab
        '
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

        Private Sub BindRoles()

            Try

                Dim objRoleController As New RoleController

                'bind dropdown for alternative content
                cboRoles.DataSource = objRoleController.GetPortalRoles(PortalId)
                cboRoles.DataBind()

                'add first item manually
                cboRoles.Items.Insert(0, New ListItem(Localization.GetString("lblGeneric.Text", Me.LocalResourceFile), "GENERIC"))

                'select the role that is currently being edited
                cboRoles.Items.FindByValue(SelectedRole).Selected = True

                'bind checkboxlist for Newsletter
                'chkRoles.DataSource = objRoleController.GetPortalRoles(PortalId)
                'chkRoles.DataBind()


            Catch ex As Exception

            End Try

        End Sub

        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        '
        ' Content Grid is the grid that displays all added
        ' content on the advanced tab
        '
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

        Private Sub BindContentGrid()


            Dim objController As New ContentController
            Dim objContentList As New ContentCollection

            objContentList = objController.ListContent(ModuleId)

            grdContent.DataSource = objContentList.GetRoleContentUnique
            grdContent.DataBind()

            DotNetNuke.Services.Localization.Localization.LocalizeDataGrid(grdContent, Me.LocalResourceFile)

            SetSortArrows()

        End Sub

        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        '
        ' used to priority move arrow iamges in the advanced grid
        '
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

        Private Sub SetSortArrows()
            Dim item As DataGridItem
            Dim rowCount As Integer = grdContent.Items.Count
            Dim i As Integer = 1
            For Each item In grdContent.Items
                If i = 1 Then
                    Dim lnkMoveUp As ImageButton = CType(item.Cells(2).Controls(1), ImageButton)
                    lnkMoveUp.Visible = False
                End If
                If i = rowCount Then
                    Dim lnkMoveDown As ImageButton = CType(item.Cells(3).Controls(1), ImageButton)
                    lnkMoveDown.Visible = False
                End If
                i = i + 1
            Next
        End Sub

#End Region

#Region "Public Methods"

        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        '
        ' returns a localized string for all users, otherwise 
        ' returns the role name itself
        '
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

        Public Function GetRolename(ByVal Rolename As String) As String

            If Rolename = "GENERIC" Then
                Return Localization.GetString("lblGeneric.Text", Me.LocalResourceFile)
            Else
                Return Rolename
            End If

        End Function

#End Region

        Protected Sub cboRoles_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboRoles.SelectedIndexChanged
            RaiseEvent RoleChanged(Me, New RoleChangedEventArgs(cboRoles.SelectedItem.Value))
        End Sub

        Protected Sub cmdContent_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim lnk As LinkButton = CType(sender, LinkButton)
            RaiseEvent RoleChanged(Me, New RoleChangedEventArgs(lnk.CommandArgument))
        End Sub

        Public Sub ContentButton_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)

            Dim btn As ImageButton = CType(sender, ImageButton)
            Dim command As String = btn.CommandName
            Dim role As String = btn.CommandArgument.Split(Char.Parse("|"))(0)
            Dim oldpos As Integer = 1
            Try
                oldpos = Convert.ToInt32(btn.CommandArgument.Split(Char.Parse("|"))(1))
            Catch
            End Try


            Select Case command.ToLower
                Case "up"
                    SetPriority(role, oldpos, -1)

                Case "down"
                    SetPriority(role, oldpos, +1)

                Case "delete"

                    Dim ctl As New ContentController
                    For Each oContent As ContentInfo In ctl.ListContent(ModuleId).Items
                        If oContent.Role.ToLower = role.ToLower Then
                            ctl.DeleteContent(oContent.ContentId)
                        End If
                    Next

                    Reorder()

            End Select

            BindContentGrid()

        End Sub

        Private Sub Reorder()

            Dim lstContent As New ContentCollection
            Dim ctl As New ContentController
            lstContent = ctl.ListContent(ModuleID)

            Dim strCurrentGridRole As String = ""
            Dim intCurrentPriority As Integer = -1


            Dim i As Integer
            For i = 0 To lstContent.Count - 1

                Dim objSortedContent As ContentInfo = CType(lstContent.GetContent(i), ContentInfo)
                Dim objPriorityController As New ContentController
                If objSortedContent.Role <> strCurrentGridRole Then

                    If intCurrentPriority = -1 Then
                        intCurrentPriority = i + 1
                    Else
                        intCurrentPriority = intCurrentPriority + 1
                    End If

                    objPriorityController.UpdatePriority(objSortedContent.ContentId, intCurrentPriority)
                    strCurrentGridRole = objSortedContent.Role

                Else
                    objPriorityController.UpdatePriority(objSortedContent.ContentId, intCurrentPriority)
                End If

            Next

        End Sub

        Public Sub SetPriority(ByVal strRole As String, ByVal OldPosition As Integer, ByVal NewPosition As Integer)

            Dim lstContent As New ContentCollection
            Dim ctl As New ContentController
            lstContent = ctl.ListContent(ModuleId)

            Dim intCurSort As Integer = OldPosition
            Dim intNewSort As Integer = OldPosition + NewPosition
            Dim intMaxSort As Integer = lstContent.HighestPriority


           If intNewSort > intMaxSort Or intNewSort < 0 Then

            Else

                'look if content is found
                If lstContent.Count > 0 Then ' content found
                    ' now loop through each content
                    For Each oContent As ContentInfo In lstContent.Items
                        If oContent.Priority = intNewSort Then
                            'found content that has the same priority than the new priority will be
                            'this content priority needs to become the priority 
                            'that the content had that is being changed
                            ctl.UpdatePriority(oContent.ContentId, intCurSort)
                        End If
                    Next
                End If
            End If

            'Now set all role content of the content to be updated with the new priority

            For Each oContent As ContentInfo In lstContent.Items
                If oContent.Role.ToLower = strRole.ToLower Then
                    ctl.UpdatePriority(oContent.ContentId, intNewSort)
                End If
            Next

        End Sub

    End Class


    Public Class RoleChangedEventArgs
        Private _rolename As String

        Public Sub New(ByVal strRole As String)
            _rolename = strRole
        End Sub

        Public Property RoleName() As String
            Get
                Return _rolename
            End Get
            Set(ByVal value As String)
                _rolename = value
            End Set
        End Property

    End Class

End Namespace
